﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto.User;
using Shop.Model.View;
using System.Text.RegularExpressions;

namespace Shop.WebApi.Controllers
{
    /// <summary>
    /// 登录服务
    /// </summary>
    [ApiController]
    [Route("Api/[controller]")]
    public class AcountController : ShopControllerBase
    {
        private readonly IAccountAppService _appService;

        private readonly IUserContext _userContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="AppService"></param>
        /// <param name="userContext"></param>
        public AcountController(IAccountAppService AppService
            , IUserContext userContext)
        {
            _appService = AppService;
            _userContext = userContext;
        }


        /// <summary>
        /// 小程序登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Login")]
        [AllowAnonymous]
        public async Task<AppResult<UseTokenInfoView>> WeChatAppletLogin(WeChatLoginDto dto)
        {
            try
            {
                var date = await _appService.WeChatAppletLogin(dto);
                return date;
            }
            catch (Exception ex)
            {
                return Problem(ex.ToString());
            }
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Register")]
        public async Task<AppResult<UseTokenInfoView>> Register(UserRegistrationDto dto)
        {
            return await _appService.Register(dto, _userContext.Id);
        }

        /// <summary>
        /// 密码登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("PasswordLogin")]
        [AllowAnonymous]
        public async Task<AppResult<UseTokenInfoView>> PasswordLogin(PasswordLoginDto dto)
        {
            if (string.IsNullOrEmpty(dto.Telphone) || !Regex.IsMatch(dto.Telphone, @"^1[3-9]\d{9}$"))
                return Problem("手机号码错误");
            if (string.IsNullOrEmpty(dto.Password))
                return Problem("密码格式错误");
            try
            {
                var token = await _appService.PasswordLogin(dto);
                return token;
            }
            catch(Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 发送登录验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("LoginCode")]
        [AllowAnonymous]
        public async Task<AppResult> SendLoginCode(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^[1]+\d{10}"))
                return Problem("请填写正确的电话号码",400);
            return await _appService.SendLoginCode(input.Value);
        }


        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("SendRegisterCode")]
        [AllowAnonymous]
        public async Task<AppResult> SendRegisterCode(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^[1]+\d{10}"))
                return Problem("请填写正确的电话号码", 400);
            return await _appService.SendRegisterCode(input.Value);
        }

        /// <summary>
        /// 注册验证
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("CheckRegisterCode")]
        [AllowAnonymous]
        public async Task<AppResult<bool>> CheckRegisterCode(PhoneCodeDto input)
        {
            if (string.IsNullOrEmpty(input.Phone) || !Regex.IsMatch(input.Phone, @"^[1]+\d{10}"))
                return Problem("请填写正确的电话号码", 400);
            return await _appService.CheckRegisterCode(input);
        }

        /// <summary>
        /// 验证码登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("SMSLogin")]
        [AllowAnonymous]
        public async Task<AppResult<UseTokenInfoView>> SMSLogin(PhoneCodeDto input)
        {
            if (string.IsNullOrEmpty(input.Phone) || !Regex.IsMatch(input.Phone, @"^[1]+\d{10}"))
                return Problem("请填写正确的电话号码", 400);
            return await _appService.SMSLogin(input);
        }

        /// <summary>
        /// 获取用户token， 仅开发使用
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet("token")]
        [AllowAnonymous]
        public async Task<UseTokenInfoView> GetUserToken([FromQuery]string phone)
            => await _appService.GetUserToken(phone);
    }
}
